//
//  KeepTrackViewController.m
//  OCPB
//
//  Created by Killerp8ss on 7/8/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "KeepTrackViewController.h"
#import "ComplainData.h"

@interface KeepTrackViewController ()

@end

@implementation KeepTrackViewController
@synthesize authorizationData;
@synthesize keepTrackTable;
NSMutableData* responseData;
NSMutableArray* complainArray;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button1 = [[UIButton alloc] init];
    button1.frame= CGRectMake(0,0,43,32);
    [button1 setBackgroundImage:[UIImage imageNamed: @"icon_back.png"] forState:UIControlStateNormal];
    
    [button1 addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
    
	// Do any additional setup after loading the view.
    [self getComplainTracking];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getComplainTracking
{
    NSURL *url = [NSURL URLWithString:@"http://www.aitproject.com/demo/webservice/public/service/complain"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    
    [request setHTTPMethod:@"GET"];
    
    

   [request setValue:authorizationData  forHTTPHeaderField:@"Authorization"];
    
    
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (connection) {
        responseData = [[NSMutableData alloc] init];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
    NSLog(@"Succeeded! Received  bytes of data");
    
    NSError *error = nil;
    NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
    
    NSString* error_code = [jsonObject valueForKey:@"error_code"];
    
    
    NSString *jsonRequest =  [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
    
    NSLog(@"jsonResponse is %@", jsonRequest);
    
    if(error_code.intValue > 0)
    {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"OCPB!"
                                                          message:[jsonObject valueForKey:@"message"]
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];
        
    }
    else
    {
        complainArray = [[NSMutableArray alloc] init];
        for(NSString *item in [jsonObject allKeys])
        {
            
            if([item isEqual: @"data"] )
            {
                NSLog(@"authorizationData is %@", authorizationData);
                
                NSDictionary *allData = [jsonObject valueForKey:item];
                
                NSArray *allComplain = [allData valueForKey:item];
                
                for(NSDictionary *complain in allComplain )
                {
                    ComplainData* complainData = [[ComplainData alloc] init];
                    complainData.idNumber = [NSString stringWithFormat:@"%@", [complain valueForKey:@"Complain_id"]];
                    complainData.code = [complain valueForKey:@"Complain_Code"];
                    complainData.subject = [complain valueForKey:@"Complain_Subject"];
                    complainData.status_id = [complain valueForKey:@"Complain_status_id"];
                    complainData.status_name = [complain valueForKey:@"Complain_Status_Name"];
                    complainData.date = [complain valueForKey:@"Complain_Date"];
                    
                    [complainArray addObject:complainData];
                    
                }
            }
        }
    }
    
    [keepTrackTable reloadData];
    
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [responseData appendData:data];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [complainArray count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"];
    //cell.textLabel.text = @"test";
    return cell;
}

- (CGFloat) tableView:(UITableView *) tableView
heightForHeaderInSection:(NSInteger) section {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    NSInteger row = indexPath.row;
    
    ComplainData* complainData = complainArray[row];
    
    UILabel *Id = (UILabel *)[cell viewWithTag:99];
    Id.text =  complainData.idNumber;
        
    UILabel *typeName = (UILabel *)[cell viewWithTag:100];
    typeName.text = complainData.subject;
        
    UILabel *personName = (UILabel *)[cell viewWithTag:101];
    personName.text = complainData.status_name;
    
    UILabel *dateLable = (UILabel *)[cell viewWithTag:105];
    dateLable.text = complainData.date;
    
    return cell;
}

@end
